package com.android.syssetting

import android.content.Intent
import android.media.AudioManager
import android.net.Uri
import android.os.Build
import android.provider.Settings
import android.util.Log
import android.view.View
import android.widget.SeekBar
import android.widget.Toast
import androidx.annotation.RequiresApi
import com.alibaba.android.arouter.facade.annotation.Route
import com.android.syssetting.databinding.ActivityBrightnessSettingBinding
import com.android.syssetting.databinding.ActivitySystemSettingBinding
import com.android.syssetting.databinding.ActivityVolumeSettingBinding
import com.android.syssetting.models.BrightnessViewModel
import com.android.syssetting.models.VolumeViewModel
import com.camera.base.Log.YLogImpl
import com.camera.base.util.ToastUtil
import com.camera.base.v.BaseActivity
import com.camera.routemodule.RouterPath

object SettingsContract {
    const val VOLUME_SETTINGS_CONTENT_URI_STRING = "content://settings/system/volume_settings"
}
data class VolumeLevels(
    val mediaVolume: Int,
    val callVolume: Int,
    val ringVolume: Int,
    val alarmVolume: Int
)
@Route(path = RouterPath.path.volume_activity)
class VolumeActivity : BaseActivity<ActivityVolumeSettingBinding, VolumeViewModel>() {
    val TAG = VolumeActivity::class.java.simpleName
    override val viewModelClass: Class<VolumeViewModel>
        get() = VolumeViewModel::class.java
    override fun getLayoutId(): Int {
        return R.layout.activity_volume_setting
    }

    override fun getStatusBarColor(): Int {
        return R.color.title_background_color
    }

    @RequiresApi(Build.VERSION_CODES.M)
    override fun loadData() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (!Settings.System.canWrite(this)) {
                val intent = Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS)
                intent.data = Uri.parse("package:" + this.packageName)
                this.startActivity(intent)
                ToastUtil.showLong("请授予应用权限")
                finish()
            } else {
                val currentVolume = getCurrentVolume()

                YLogImpl.i(TAG, "  111111111111   $currentVolume.callVolume   $currentVolume.mediaVolume   $currentVolume.ringVolume   $currentVolume.alarmVolume")
                setVolumeProgress(currentVolume)
            }
        } else {
            val currentVolume = getCurrentVolume()
            YLogImpl.i(TAG, "")
            setVolumeProgress(currentVolume)
        }
    }

    override fun initView() {
        mBinding.sbActivityVolumeSettingMedia.setOnSeekBarChangeListener(
            createSeekbarChangeListener(
                SettingsContract.VOLUME_SETTINGS_CONTENT_URI_STRING,
                AudioManager.STREAM_MUSIC
            )
        )
        mBinding.sbActivityVolumeSettingCall.setOnSeekBarChangeListener(
            createSeekbarChangeListener(
                SettingsContract.VOLUME_SETTINGS_CONTENT_URI_STRING,
                AudioManager.STREAM_VOICE_CALL
            )
        )
        mBinding.sbActivityVolumeSettingRing.setOnSeekBarChangeListener(
            createSeekbarChangeListener(
                SettingsContract.VOLUME_SETTINGS_CONTENT_URI_STRING,
                AudioManager.STREAM_RING
            )
        )
        mBinding.sbActivityVolumeSettingNz.setOnSeekBarChangeListener(
            createSeekbarChangeListener(
                SettingsContract.VOLUME_SETTINGS_CONTENT_URI_STRING,
                AudioManager.STREAM_ALARM
            )
        )
    }

    private fun createSeekbarChangeListener(
        contentUriString: String,
        streamType: Int
    ): SeekBar.OnSeekBarChangeListener {
        return object : SeekBar.OnSeekBarChangeListener {
            @RequiresApi(Build.VERSION_CODES.M)
            override fun onProgressChanged(seekBar: SeekBar, progress: Int, fromUser: Boolean) {
                setVolume(contentUriString, progress, streamType)
            }

            override fun onStartTrackingTouch(seekBar: SeekBar) {}

            override fun onStopTrackingTouch(seekBar: SeekBar) {}
        }
    }

    @RequiresApi(Build.VERSION_CODES.M)
    private fun getCurrentVolume(): VolumeLevels {
        val audioManager = this@VolumeActivity.getSystemService(AudioManager::class.java)
        val maxMediaVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC)
        val mediaVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC)

        val maxCallVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_VOICE_CALL)
        val callVolume = audioManager.getStreamVolume(AudioManager.STREAM_VOICE_CALL)

        val maxRingVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_RING)
        val ringVolume = audioManager.getStreamVolume(AudioManager.STREAM_RING)

        val maxAlarmVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_ALARM)
        val alarmVolume = audioManager.getStreamVolume(AudioManager.STREAM_ALARM)

        return VolumeLevels(
            mediaVolume = (mediaVolume * 100) / maxMediaVolume,
            callVolume = (callVolume * 100) / maxCallVolume,
            ringVolume = (ringVolume * 100) / maxRingVolume,
            alarmVolume = (alarmVolume * 100) / maxAlarmVolume
        )
    }

    private fun setVolumeProgress(volumes: VolumeLevels) {
        mBinding.sbActivityVolumeSettingMedia.progress = volumes.mediaVolume
        mBinding.sbActivityVolumeSettingCall.progress = volumes.callVolume
        mBinding.sbActivityVolumeSettingRing.progress = volumes.ringVolume
        mBinding.sbActivityVolumeSettingNz.progress = volumes.alarmVolume
    }

    @RequiresApi(Build.VERSION_CODES.M)
    private fun setVolume(contentUriString: String, progress: Int, streamType: Int) {
        val audioManager = getSystemService(AudioManager::class.java)
        val maxVolume = audioManager.getStreamMaxVolume(streamType)
        val actualVolume = (progress * maxVolume) / 100 // Calculate actual volume from percentage
        Log.e(TAG, "setVolume: $actualVolume      streamType:$streamType" )
        when (streamType) {
            AudioManager.STREAM_MUSIC -> audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, actualVolume, 0)
            AudioManager.STREAM_VOICE_CALL -> audioManager.setStreamVolume(AudioManager.STREAM_VOICE_CALL, actualVolume, 0)
            AudioManager.STREAM_RING -> audioManager.setStreamVolume(AudioManager.STREAM_RING, actualVolume, 0)
            AudioManager.STREAM_ALARM -> audioManager.setStreamVolume(AudioManager.STREAM_ALARM, actualVolume, 0)
        }
    }
}